//
//  DBJLoginModel.m
//  DBJ
//
//  Created by 花化华 on 2016/11/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJLoginModel.h"

@implementation DBJLoginModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

- (NSString *)description{
    return [NSString stringWithFormat:@"姓名: %@, 年龄: %@, 性别: %@, 身高: %@, 体重: %@, 籍贯: %@, 联系方式: %@, 证件号码: %@,,常居地: %@",_true_name,_age,_dbj_gender,_height,_weight,_address,_mobile_number,_card_number,_habitual_residence];
}

+ (instancetype)loginWithDict:(NSDictionary *)dict{
    DBJLoginModel *loginModel = [[DBJLoginModel alloc]init];
    [loginModel setValuesForKeysWithDictionary:dict];
    
    //初始化一些其它数据
    loginModel.dbj_gender = @"男";
    loginModel.habitual_residence = @"广东深圳";
    
    return loginModel;
}

#pragma mark - 归档解档相关
/**
 * encode是编码的意思, coder编码器
 * 该方法是归档的关键操作
 * 将对象上的属性保存起来
 
 要做的事情: 将本对象所有的属性进行归档操作
 */
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    // NSCoder 表示编码器, 能够将属性编码成二进制数据, 可以保存起来
    
    /*------ 将本对象所有的属性进行归档操作 ------*/
    // 不同类型的数据, 有不同的归档方法
    [aCoder encodeObject:_true_name forKey:@"true_name"];
    [aCoder encodeObject:_age forKey:@"age"];
    [aCoder encodeObject:_dbj_gender forKey:@"dbj_gender"];
    [aCoder encodeObject:_height forKey:@"height"];
    [aCoder encodeObject:_weight forKey:@"weight"];
    [aCoder encodeObject:_address forKey:@"address"];
    [aCoder encodeObject:_habitual_residence forKey:@"habitual_residence"];
    [aCoder encodeObject:_mobile_number forKey:@"mobile_number"];
    [aCoder encodeObject:_card_number forKey:@"card_number"];
}

/**
 * 该方法是解档的关键操作
 * 获取已经保存的对象属性, 还原到对象身上
 
 要做的事情:
 1. 实例化一个本对象
 2. 将解档还原回来的属性, 赋值到本对象身上
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    // NSCoder 也是解码器, 能够将二进制数据还原成属性
    
    /*------ 1. 实例化对象 ------*/
    self = [super init];
    if (self) {
        /*------ 2. 将解档出来的属性, 还原到自己身上 ------*/
        _true_name = [aDecoder decodeObjectForKey:@"true_name"];
        _age = [aDecoder decodeObjectForKey:@"age"];
        _dbj_gender = [aDecoder decodeObjectForKey:@"dbj_gender"];
        _height = [aDecoder decodeObjectForKey:@"height"];
        _weight = [aDecoder decodeObjectForKey:@"weight"];
        _address = [aDecoder decodeObjectForKey:@"address"];
        _habitual_residence = [aDecoder decodeObjectForKey:@"habitual_residence"];
        _mobile_number = [aDecoder decodeObjectForKey:@"mobile_number"];
        _card_number = [aDecoder decodeObjectForKey:@"card_number"];
    }
    return self;
}

@end
